<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/4/24
 * Time: 8:54
 */

namespace app\admin\model;

use app\index\model\Cities;
use app\index\model\Provinces;
use think\Db;
use think\Model;

class MoneyLog extends Model{
    protected $pk = 'id';
    protected $autoWriteTimestamp = true;

    /**
     * @param array $where
     * @param int $pageSize
     * @param string $field
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($where= [], $pageSize=10,$field=''){
        $field = $field ? $field: "{$this->getTable()}.*,think_article.title,think_mch.name as mc_name";
        $list = $this
            ->field($field)
            ->join('think_article',"think_article.id = {$this->getTable()}.aid")
            ->join('think_mch',"think_mch.id = {$this->getTable()}.mc_id")
            ->order($this->getTable().'.create_time desc')
            ->where($where)
            ->paginate($pageSize);
        return $list;
    }

    /**
     * 添加日志
     * @param string $name
     * @param string $mc_id
     * @param string $aid
     * @param string $money
     * @param string $changes
     * @param int $type
     * @return bool
     */
    public function addNew($name='',$mc_id='',$aid='',$money='',$changes='',$type=1){
        $str = substr($changes,0,1);
        $str1 = substr($changes,1);
        $data = [
            'aid'  => $aid,
            'mc_id' => $mc_id,
            'name' => $name,
            'money'=> $money,
            'changes'=> $str.round($str1,2),
            'type'=>$type,
            'create_time'=>time()
        ];
        $validate = new \app\admin\validate\MoneyLog();
        if(!$validate->check($data)){
            return false;
        }
        if(!$this->insert($data)){
            return false;
        }
        return true;
    }
}